<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button style="float:right" type="primary" @click="handleAdd" size="small">
          新增资讯
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="100px">
          <el-form-item label="搜索标题：">
            <el-input v-model="listQuery.title" placeholder="请输入搜索标题" clearable></el-input>
          </el-form-item>
          <el-form-item label="状态：">
            <el-select v-model="listQuery.status" placeholder="请选择" clearable>
              <el-option label="已上架" :value="1"></el-option>
              <el-option label="未上架" :value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-button type="primary" size="small" @click="handleSearchList">查询</el-button>
        </el-form>
      </div>
    </el-card>
    
    <div class="table-container">
      <el-table :data="list" style="width: 100%;" v-loading="listLoading" border>
        <el-table-column label="资讯标题" align="center" min-width="200">
          <template slot-scope="scope">{{ scope.row.title }}</template>
        </el-table-column>
        <el-table-column label="创建日期" width="160" align="center">
          <template slot-scope="scope">{{ scope.row.created_at }}</template>
        </el-table-column>
        <el-table-column label="阅读量" width="100" align="center">
          <template slot-scope="scope">{{ scope.row.view_count }}</template>
        </el-table-column>
        <el-table-column label="上架状态" width="120" align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.is_published ? 'success' : 'danger'">
              {{ scope.row.is_published ? '已上架' : '未上架' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否置顶" width="120" align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.is_pinned ? 'success' : 'info'">
              {{ scope.row.is_pinned ? '已置顶' : '未置顶' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="250" align="center">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleView(scope.row)">编辑</el-button>
            <el-button size="mini" @click="handlePublishToggle(scope.row)">
              {{ scope.row.is_published ? '下架' : '上架' }}
            </el-button>
            <el-button size="mini" @click="handlePinToggle(scope.row)">
              {{ scope.row.is_pinned ? '取消置顶' : '置顶' }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5, 10, 15]"
        :current-page.sync="listQuery.pageNum"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { fetchAnnouncementList, deleteAnnouncement, updateAnnouncement, updateAnnouncementPublishStatus, updateAnnouncementPinStatus } from '@/api/content'

export default {
  name: 'announcementList',
  data() {
    return {
      listQuery: {
        title: '',
        status: '',
        pageNum: 1,
        pageSize: 10
      },
      list: [],
      total: 0,
      listLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchAnnouncementList(this.listQuery).then(response => {
        this.listLoading = false
        // 详细日志记录时间数据
        if (response.data && response.data.data) {
          this.list = response.data.data
          this.total = response.data.total
        } else {
          this.list = []
          this.total = 0
        }
      }).catch(error => {
        this.listLoading = false
        console.error('获取列表失败:', error)
        this.$message.error('获取列表失败')
      })
    },
    handleSearchList() {
      this.listQuery.pageNum = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1
      this.listQuery.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val
      this.getList()
    },
    handleAdd() {
      this.$router.push('/content/announcement/add')
    },
    handleView(row) {
      this.$router.push(`/content/announcement/edit/${row.id}`)
    },
    handlePublishToggle(row) {
      const actionText = row.is_published ? '下架' : '上架'
      this.$confirm(`确定要${actionText}该公告吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 使用专用的状态更新接口
        const newStatus = row.is_published ? 0 : 1
        updateAnnouncementPublishStatus(row.id, newStatus).then(() => {
          this.$message.success(`${actionText}成功`)
          this.getList()
        }).catch(error => {
          console.error(`${actionText}失败:`, error)
          this.$message.error(`${actionText}失败: ${error.response && error.response.data && error.response.data.message ? error.response.data.message : '未知错误'}`)
        })
      })
    },
    handlePinToggle(row) {
      const actionText = row.is_pinned ? '取消置顶' : '置顶'
      this.$confirm(`确定要${actionText}该公告吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 使用专用的状态更新接口
        const newStatus = row.is_pinned ? 0 : 1
        updateAnnouncementPinStatus(row.id, newStatus).then(() => {
          this.$message.success(`${actionText}成功`)
          this.getList()
        }).catch(error => {
          console.error(`${actionText}失败:`, error)
          this.$message.error(`${actionText}失败: ${error.response && error.response.data && error.response.data.message ? error.response.data.message : '未知错误'}`)
        })
      })
    }
  }
}
</script>

<style scoped>
.app-container {
  padding: 20px;
}
.filter-container {
  margin-bottom: 20px;
}
.table-container {
  margin-bottom: 20px;
}
</style>
